﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.Script.Serialization;

namespace ConsolidationTool.Common
{

    /// <summary>
    /// 导出Report的配置信息, 如Mapping数据,附加数据
    /// </summary>
    public class ReportConfig
    {
        public ReportConfig(SPListItem item)
        {
            this.Title = item["Title"] + "";
            this.Description = item["Description"] + "";
            this.Category = item["Category"] + "";
            this.ExcelTemplate = item["ExcelTemplate"] + "";
            this.Mapping = BusinessService.RefJsonData<ARTReportRow>(item["Mapping"] + "");
            this.GroupMapping = BusinessService.RefJsonData<GroupMap>(item["GroupMapping"] + "");
            this.GroupMapping.ForEach(p =>
            {
                p.HorizontalIndex = p.Index.Split('|')[0];
                p.VerticalIndex = int.Parse(p.Index.Split('|')[1]);
            });
            this.AdditionOutput = BusinessService.RefJsonData<AdditionMap>(item["AdditionOutput"] + "");


            //(item["GroupMapping"] + "").Split(',').ToList().ForEach(p =>
            //{
            //    string gn = p.Split(':')[0];
            //    string index = p.Split(':')[1];
            //    string hi = index.Split('|')[0];
            //    string vi = index.Split('|')[1];
            //    this.GroupMapping.Add(new GroupMap()
            //    {
            //        GroupName = gn,
            //        Index = index,
            //        HorizontalIndex = hi,
            //        VerticalIndex = int.Parse(vi)
            //    });
            //});
            //(item["AdditionOutput"] + "").Split(',').ToList().ForEach(p =>
            //{
            //    string index = p.Split(':')[0];
            //    string value = p.Split(':')[1];
            //    this.AdditionOutput.Add(new AdditionMap()
            //    {
            //        Index = index,
            //        Value = value
            //    });
            //});
        }


        public string Title { get; set; }
        public string Description { get; set; }
        public string Category { get; set; }
        public List<ARTReportRow> Mapping { get; set; }
        /// <summary>
        /// 由于Mapping中可输入多个组,这里的GroupMapping指的是每一个分组所在excel中的位置信息,如 Table1:H|12
        /// </summary>
        public List<GroupMap> GroupMapping { get; set; }
        public List<AdditionMap> AdditionOutput { get; set; }



        public string ExcelTemplate { get; set; }
    }

    public class GroupMap
    {
        public string GroupName { get; set; }
        public string HorizontalIndex { get; set; }
        public int VerticalIndex { get; set; }

        public string List { get; set; }
        public string Index { get; set; }
    }

    public class AdditionMap
    {
        public string Index { get; set; }
        public string Template { get; set; }
        public string Value { get; set; }
    }

    /// <summary>
    /// 导出的Report项
    /// </summary>
    public class OutputReportItem
    {
        public OutputReportItem()
        {
            MainDataRow = new Dictionary<GroupMap, List<RQDataItem>>();
            AdditionOutput = new Dictionary<string, string>();
        }

        public string SheetName { get; set; }
        public string ExcelTemplate { get; set; }
        public string Title { get; set; }
        /// <summary>
        /// 用于导出的有序列表,其中可包含空行,对应excel模板中的行与列.
        /// </summary>
        public Dictionary<GroupMap, List<RQDataItem>> MainDataRow { get; set; }
        /// <summary>
        /// 任意的导出位置,指定表格定位然后写入相关数据
        /// </summary>
        public Dictionary<string, string> AdditionOutput { get; set; }
    }

    public class ARTReportRow
    {
        public int id { get; set; }
        public string title { get; set; }
        public string group { get; set; }
        public string list { get; set; }
        public string equ { get; set; }
        public List<ARTReportItem> fields { get; set; }
        public bool titleRow { get; set; }
        public bool emptyRow { get; set; }
        public int fieldCount { get; set; }
        public bool M12 { get; set; }
        public bool Budget { get; set; }
        public bool withCurrentMonth { get; set; }
        public string[] equFields { get; set; }
        public bool avg { get; set; }
        public bool skip { get; set; }
        /// <summary>
        /// rolling 12 months
        /// </summary>
        public bool R12 { get; set; }

        public List<RQDataItem> Datasource { get; set; }

        public string identity { get; set; }

        public int[] index { get; set; }
        public bool currentGroup { get; set; }

        public bool bold { get; set; }

        public string unit { get; set; }

        public bool subrow { get; set; }

        public bool compress { get; set; }

        public string digits { get; set; }
        /// <summary>
        /// 导出到report里面的横向起始位置，默认为空，特殊情况特殊处理
        /// </summary>
        public string rcol { get; set; }
        /// <summary>
        /// 用于其它条件的情况
        /// </summary>
        public string otherExp { get; set; }
        public bool onlycm { get; set; }
        //dengshuai 2014-10-30
        public int month { get; set; }
    }

    public class ARTReportItem : ICloneable
    {
        public string name { get; set; }
        public string list { get; set; }
        public string equ { get; set; }
        public string digits { get; set; }
        public bool emptycolumn { get; set; }
        public List<ARTReportFromItem> from { get; set; }

        public string value { get; set; }

        public bool selfrow { get; set; }
        public bool selftable { get; set; }
        public int[] index { get; set; }

        public string method { get; set; }
        //dengshuai 2014-10-30
        public string type { get; set; }

        public object Clone()
        {
            var item = new ARTReportItem();

            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            string itemStr = Serializer.Serialize(this);
            return Serializer.Deserialize<ARTReportItem>(itemStr);
        }

        public int id { get; set; }

        public List<string> express { get; set; }
    }

    [Serializable]
    public class ARTReportFromItem : ICloneable, IEncryptionCompressed
    {
        /// <summary>
        /// the identity for this receive
        /// </summary>
        public string identity { get; set; }
        /// <summary>
        /// also be empty of form parent
        /// </summary>
        public string list { get; set; }
        /// <summary>
        /// maybe need to Positioning
        /// </summary>
        public string field { get; set; }
        /// <summary>
        /// use to location the method
        /// </summary>
        public string method { get; set; }

        public string profix { get; set; }
        public string suffix { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        public bool budget { get; set; }

        public bool komode { get; set; }
        public int month { get; set; }

        public bool isConsolidationWay
        {
            get
            {
                return list.StartsWith("R_");
            }
        }

        public string Encryption()
        {
            return identity + list + field + method;
        }

        public string equ { get; set; }

        public string merge { get; set; }
    }
}
